local resty_rsa = require "rsa"

local rsa_priv_key = [[-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKSXbKBuBozwBzBq
Q77wQOKBZnxcUVpxgPr44oV/waVjX1qYjCvJtiP6HkZKlVW4584b8AWmYUviKD/v
fmYkI7PFoY1dSxuLX3IpW5Iio8X45koy4UZkO75pd8IohbBKgWKHqFuJXO8n1xzY
qOc1CWqkeCtS4Q20dwf4QMlOEfBlAgMBAAECgYAS/ITDXO3UhDvQEfIOrJhPiK3R
2rRnwoAmc19uFva3pWbyhHZm9rXUIJ4tbZ0cbmAPHpnpjO/cWRJbjbz++1xCyS2A
vQPiuKdoOy+TeTzOu1Cmhc9bkCaeXaBOr+ojJNLqJx+RyADEt8lSQb14Fo0OMozl
qFGUuVjGHHjxG4uAAQJBANqukjgIAUkNfcHUkBWIaBBDRTEpZ3mB24MmW2ey6SBB
OeDK8mrHISz7hCjkIEtt6fAbnaPqoSdyLgAjTJ7+HGUCQQDArdY3zC/G/SIZPnPN
KkRwpjVHaUNv703C4ATzdCFo5OZ7u8nYVfu+PiJ4g7MAMFYvyXKwEm1DsJ68R0ma
VUQBAkBXWZfqbbgluA/dlsye7Ue0B0riIknEzUwjjPTB7UMVsTgvRKGkaTolP6pA
sy77MkJZuI41ZYlNulwJjhATCinJAkEAuOfalU+JZjBUgd9SKEjwxx5qMO7OvePs
PyDW/3+p9Ez+9y1e5hKV9ZsRX5AbvwmkHFX5jwkP/I7Jtqu+8bH0AQJBAJntT7wt
DvqCjSzHG8HQrrhUivh90KYysiq+fEEgaPa2zNTYcgQ3KIZPGHfePGxLodMz1OcB
wPbDN8/DnGYiXy0=
-----END PRIVATE KEY-----]]

local priv, err = resty_rsa:new({
    private_key = rsa_priv_key,
    key_type = resty_rsa.KEY_TYPE.PKSC8,
})

if not priv then
    ngx.log(ngx.ERR, "new rsa err" .. err)
    return
end


local function access()
    local priv, err = resty_rsa:new({
        private_key = rsa_priv_key,
        key_type = resty_rsa.KEY_TYPE.PKSC8,
    })
 
    if not priv then
        ngx.log(ngx.ERR, "new rsa err" .. err)
        return true
    end
    
    ngx.req.read_body()

    local body_data = priv:decrypt(ngx.decode_base64(ngx.req.get_body_data()))

    if body_data then
        ngx.log(ngx.INFO, body_data)
        ngx.req.set_body_data(body_data)
    end
    return true
end

if access() then
    return
end
